package com.tianwu.lbsdatabasedesign;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.sql.Time;
import java.util.ArrayList;

/**
 * Created by Tianwu on 2016/7/5.
 */
public class SaveLocation {

    public static boolean addLocation(Context context, MyLocation location){

        SQLiteDatabase db = new Database(context).getReadableDatabase();

        ContentValues values = new ContentValues();
        values.put(MyLocation.TIME,location.time);
        values.put(MyLocation.ADDRESS,location.address);
        values.put(MyLocation.LATITUDE,location.latitude);
        values.put(MyLocation.LONGITUDE,location.longitude);

        return db.insert(Database.TABLE_NAME, null, values) > 0;
    }
    public static ArrayList<MyLocation> getAllLocation(Context context){
        SQLiteDatabase db = new Database(context).getReadableDatabase();

        ArrayList<MyLocation> locations = new ArrayList<>();

        String[] projection = {"*"};

        Cursor cursor = db.query(
                Database.TABLE_NAME,
                projection,
                null,
                null,
                null,
                null,
                null
            );
        int count = cursor.getCount();
        if(count <= 0) return locations;
        cursor.moveToFirst();
        for(int i = 0; i < count; i++){
            locations.add(new MyLocation(
                    cursor.getString(0),
                    cursor.getString(1),
                    cursor.getDouble(2),
                    cursor.getDouble(3)
            ));
            cursor.moveToNext();
        }
        return locations;
    }
}
